package com.heihei.validator;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;


@Aspect
@Component
public class ValidateAspect {

	//@Before    之前调用
	//@After     成功返回后，调用
	//@AfterT    抛出异常时，调用
	//@AfterThrowing
	//@Around    包含了上述作用（全程）

	//具体切面表达式：  spring官方网站 -- 上方菜单 Projects -- Spring Framework

	@Around("execution(* com.heihei.web.controller.UserController.*(..))")
	public Object handleValidateResult(ProceedingJoinPoint pjp) throws Throwable {
		
		System.out.println("进入切片");

		Object[] args = pjp.getArgs();
		for (Object arg : args) {
			if(arg instanceof BindingResult) {
				BindingResult errors = (BindingResult)arg;
				if (errors.hasErrors()) {
					throw new ValidateException(errors.getAllErrors());
				}
			}
		}
		
		Object result = pjp.proceed();
		
		return result;
	}

}
